package com.example.task1.ui.AllService.law;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.LawMyOrderBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class LawOrderAdapter extends BaseAdapter<LawMyOrderBean.RowsDTO> {

    private Context context;
    private List<LawMyOrderBean.RowsDTO> mData = new ArrayList<>();
    private TextView tvName;
    private TextView tvContent;
    private TextView tvPhone;
    private ImageView image;

    public LawOrderAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void setData(List<LawMyOrderBean.RowsDTO> rows) {
        mData = rows;
        notifyDataSetChanged();
    }

    @Override
    protected int getLayout() {
        return R.layout.laworder;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<LawMyOrderBean.RowsDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, LawMyOrderBean.RowsDTO date) {
        tvName.setText(date.getLawyerName());
        tvContent.setText(date.getContent());
        tvPhone.setText(date.getPhone());
        SharedPreferences sharedPreferences = MyApplication.getSharedPreferences();
        boolean aBoolean = sharedPreferences.getBoolean("law_" + date.getId(), false);
        if (aBoolean) {
            image.setImageResource(R.drawable.ic_baseline_favorite_red_24);
        }
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("adviceId", date.getId());
                if (aBoolean) {
                    map.put("like", false);
                } else {
                    map.put("like", true);
                }
                Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/lawyer-consultation/legal-advice/evaluate-like", Api.getBody(map));
                Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp rows) {
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        if (aBoolean) {
                            edit.putBoolean("law_" + date.getId(), false);
                            image.setImageResource(R.drawable.ic_baseline_favorite_24);
                        } else {
                            edit.putBoolean("law_" + date.getId(), true);
                            image.setImageResource(R.drawable.ic_baseline_favorite_red_24);
                        }
                        edit.commit();
                        MyApplication.show("点赞成功");
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(context, LawOrderDetailActivity.class, date.getId());
            }
        });
    }

    @Override
    protected void holder(View view) {
        initView(view);
    }

    private void initView(View view) {
        tvName = view.findViewById(R.id.tv_name);
        tvContent = view.findViewById(R.id.tv_content);
        tvPhone = view.findViewById(R.id.tv_phone);
        image = view.findViewById(R.id.image);
    }
}
